package com.woniu.smallmedical.controller;

import com.woniu.smallmedical.entity.MedicineDiarys;
import com.woniu.smallmedical.entity.PageBean;
import com.woniu.smallmedical.entity.ResponseResult;
import com.woniu.smallmedical.mapper.AdjustmentMedicineMapper;
import com.woniu.smallmedical.mapper.MedicineDiarysMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


@CrossOrigin
@RestController
public class MedicineDiarysController {

    @Autowired
    MedicineDiarysMapper basicmessageapper;

    @Autowired
    AdjustmentMedicineMapper adasicmessageapper;

    @GetMapping("/medicines")
    public ResponseResult medicine(Integer pageIndex, Integer pageSize){
        List<MedicineDiarys> basicmessages = basicmessageapper.selectAll(pageIndex,pageSize);
        System.out.println("pageIndex,"+pageIndex);
        PageBean<MedicineDiarys> pageBean=new PageBean<>();
        pageBean.setPageBeanList(basicmessages);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord(basicmessageapper.selectcount());
        return new ResponseResult<PageBean<MedicineDiarys>>(pageBean,200,"OK");
    }


    //通过id查
    @GetMapping("/medicineinsertByids")
    public MedicineDiarys selectbuid( Integer smid){
        System.out.println("Id是"+smid);
        MedicineDiarys basicmessageapper = selectbuid(smid);

        return basicmessageapper;
    }
    //通过id修改
    @GetMapping("/updates")
    public void updatebuid( Integer smid){
        System.out.println("需要修改状态的ID是"+smid);
        basicmessageapper.update(smid);


    }
    //调价
    @GetMapping("/updateDiarys")
    public void updateDiarys( Integer smid,Double retail,Integer count,Double fretail){
        System.out.println("需要调价的ID是"+smid);
      Integer counts=basicmessageapper.selectDiarys(smid)+count;
        System.out.println("counts,"+counts);
        System.out.println("没改之前的retial是："+fretail);
        //修改调价表之后加到调价信息表理
        basicmessageapper.updateDiarys(smid,retail,counts);
        //先查出来要改的对象
        MedicineDiarys basicmessageappers=basicmessageapper.selectById(smid);
        //再加入信息表
        adasicmessageapper.insertmessage(basicmessageappers.getNumbers(),basicmessageappers.getNickname(),basicmessageappers.getMedicinespecs(),basicmessageappers.getCount(),basicmessageappers.getPurchase(),fretail, retail,new Date(),smid);


    }

    //查询信息通过编号
    @GetMapping("/Bynicknamaes")
    public ResponseResult selectbuid( String nicknames,Integer pageIndex,Integer pageSize ){

        System.out.println("名字是"+nicknames);
        if(nicknames.equals("") || nicknames==null){
            List<MedicineDiarys> basicmessages = basicmessageapper.selectAll(pageIndex,pageSize);
            PageBean<MedicineDiarys> pageBean=new PageBean<>();
            pageBean.setPageBeanList(basicmessages);
            pageBean.setPageIndex(pageIndex);
            pageBean.setPageSize(pageSize);
            pageBean.setTotalRecord(basicmessageapper.selectcount());
            return new ResponseResult<PageBean<MedicineDiarys>>(pageBean,200,"OK");
        }else {
            List<MedicineDiarys> basicmessages =basicmessageapper.SelctNumber(nicknames);

            PageBean<MedicineDiarys> pageBean=new PageBean<>();
            pageBean.setPageBeanList(basicmessages);
            pageBean.setPageIndex(pageIndex);
            pageBean.setPageSize(pageSize);
            pageBean.setTotalRecord(basicmessageapper.selectcount());
            return new ResponseResult<PageBean<MedicineDiarys>>(pageBean,200,"OK");
        }


    }


}
